import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store"

Vue.use(VueRouter)
const Home = () => import('@/pages/Home')

const Login = () => import('@/pages/Login')

const Search = () => import('@/pages/Search')

const Register = () => import('@/pages/Register')

const Detail = () => import('@/pages/Detail')

const AddCartSuccess = () => import('@/pages/AddCartSuccess')

const ShopCart = () => import('@/pages/ShopCart')
const Center = () => import('@/pages/Center')
const Pay = () => import('@/pages/Pay')
const PaySuccess = () => import('@/pages/PaySuccess')
const Trade = () => import('@/pages/Trade')
const TeamOrder = () => import('@/pages/Center/TeamOrder')
const MyOrder = () => import('@/pages/Center/MyOrder')


//push重写
const last = VueRouter.prototype.push
VueRouter.prototype.push = function (location, onComplete = () => {}, onAbort = () => {}) {
    last.call(this, location, onComplete, onAbort)
}

const router = new VueRouter({
    scrollBehavior() {
        return {
            x: 0,
            y: 0
        }
    },
    mode: "history",
    routes: [{
            path: '/home',
            component: Home,
            name: 'home'
        },
        {
            path: '/search/:keyword?',
            component: Search,
            name: 'search'
        },
        {
            path: '/login',
            component: Login,
            name: 'login',
            meta: {
                isHidden: true
            },
            /* beforeEnter(to,from,next){
                if(store.state.user.token){
                    next('/')
                }else{
                    next()
                }
            } */
        },
        {
            path: '/register',
            component: Register,
            name: 'register',
            meta: {
                isHidden: true
            },
        },
        {
            path: '/detail/:skuId',
            component: Detail,
            name: 'detail',
        },
        {
            path: '/addcartsuccess',
            component: AddCartSuccess,
            name: 'addcartsuccess',
            beforeEnter(to,from,next){
                if(sessionStorage.getItem("cartData")){
                    next()
                }else{
                    next('/shopcart')
                }
            }
        },
        {
            path: '/shopcart',
            component: ShopCart,
            name: 'shopcart',
        },
        {
            path: '/center',
            component: Center,
            // name: 'center',
            children:[
                {
                    path:'myorder',
                    component:MyOrder,
                    name:'myorder'
                },
                {
                    path:'teamOrder',
                    component:TeamOrder,
                    name:'teamOrder'
                },
                {
                    path:'',
                    redirect:'/center/myorder'
                },
                
            ]
        },
        {
            path: '/pay',
            component: Pay,
            name: 'pay',
            beforeEnter(to,from,next){
                if(from.path.startsWith('/trade')){
                    
                    next()
                }else{
                    next("/trade")
                }
            }
        },
        {
            path: '/paysuccess',
            component: PaySuccess,
            name: 'paysuccess',
            beforeEnter(to,from,next){
                if(from.path.startsWith('/pay')){
                    next()
                }else{
                    next("/shopcart")
                }
            }
        },
        {
            path: '/trade',
            component: Trade,
            name: 'trade',
            beforeEnter(to,from,next){
                if(from.path.startsWith('/shopcart')||sessionStorage.getItem('route')==='/trade'){
                    sessionStorage.setItem('route','/trade')
                    next()
                }else{
                    next("/shopcart")
                }
            },
        },
        {
            path: '/',
            redirect: "/home"
        },
    ]
})
//权限控制(判断是否拥有token,获取用户名)
router.beforeEach((to, from, next) => {
    //需要路由守卫判断的路由
    const checkedPath = ['/center', "/pay", "/paysuccess", "/trade",'/shopcart']
    const isChecked = checkedPath.some(item => {
        return to.path.startsWith(item)
    })

    if (isChecked) {
        
        //是否有用户名
        if (store.state.user.userName) {
          return  next()
        }
        //是否拥有token
        if(store.state.user.token){
            //有就获取用户名
           store.dispatch("user/getUserName")
           next()
        }else{
            next('/login')
        }
    } else {
        //尽管访问的是不需要路由守卫的路由,但是我们为了差异化,仍然判断,有就获取用户名
        if(store.state.user.token){
            store.dispatch("user/getUserName")
        }
        next()
    }



})


export default router